//author:jxx

//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
    gridBody: () => import('./hotel/edit.vue'),//编辑
    gridFooter: () => import('./hotel/room.vue'),//房型详情
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: '',
    // modelBody: () => import('@/views/mall/Rooms.vue'),
    modelFooter: '',
  },
  buttons: { view: [], box: [], detail: [] },//扩展的按钮
  data () {
    return {
      datalist: []
    }
  },
  methods: {//事件扩展
    getFormOption (field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },
    modelOpenAfter (row) {
      // this.boxOptions.height = document.documentElement.clientHeight * 0.8;
      let isEDIT = this.currentAction == this.const.EDIT;
      let HOTELDETAILADDRESS = this.getFormOption('HOTELDETAILADDRESS')
      let HOTELDETAILADDRESS2 = this.getFormOption('HOTELDETAILADDRESS2')
      let distributorNews_id = this.getFormOption('distributorNews_id')
      if (!isEDIT) {
        // 新增
        this.hasDetail = false
        this.detail.isDetail = true
        this.detail.isDetails = false
        this.editFormFields.HOTELDETAILADDRESS = ""
        this.editFormFields.hotel_address = ""
        this.editFormFields.hotel_Location = ""
        HOTELDETAILADDRESS.data = []
        HOTELDETAILADDRESS.hidden = false
        HOTELDETAILADDRESS.required = true
        HOTELDETAILADDRESS2.hidden = true
        this.editFormOptions[2][1].extra = {
          render: h => {
            return h(
              "div",
              {
                style: {
                  "border": "1px solid #dcdee2",
                  "width": "200px",
                  "height": "32px",
                  "padding": "0px 10px"
                },
              },
              this.editFormFields.hotel_Location
            )
          }
        }
        this.editFormFields.is_open = 0
      } else {
        // 编辑
        this.hasDetail = true
        this.detail.isDetail = true
        this.detail.isDetails = true
        this.editFormFields.hotel_Location = row.hotel_Location
        HOTELDETAILADDRESS.hidden = true
        HOTELDETAILADDRESS.required = false
        HOTELDETAILADDRESS2.hidden = false
        this.editFormOptions[2][1].extra = {
          text: "修改地址", //显示文本
          //触发事件
          click: (item) => {
            HOTELDETAILADDRESS.hidden = false
            HOTELDETAILADDRESS2.hidden = true
            HOTELDETAILADDRESS.required = true
          },
        }
        // this.editFormFields.HOTELDETAILADDRESS = row.hotel_address + ' - ' + row.hotel_detailaddress
        this.editFormFields.HOTELDETAILADDRESS2 = row.hotel_address + ' - ' + row.hotel_detailaddress + '|' + row.hotel_Location
        if (row.is_open == 1) {
          distributorNews_id.hidden = false
        } else {
          distributorNews_id.hidden = true
        }
        if (row.is_dy) {
          this.setHi(row.is_dy)
        }
        this.DYHomeCode(row.dy_hotel_ID)
      }
    },
    getSelListFun (data) {
      this.http.get("api/Hotels/GetRoomTypes?supplierCode=" + this.editFormFields.supplier_Name + "&hotelsID=" + this.editFormFields.hotel_ID, false).then(reslut => {
        if (reslut.status) {
          this.datalist = reslut.data.map(j => {
            return {
              key: j.dicValue,
              value: j.dicName
            }
          })
          this.detail.columns.forEach(x => {
            if (x.field == "pms_Room_Code") {
              x.edit = { type: "selectList", keep: true };
              x.bind = { key: "pms_Room_Code", data: this.datalist };
            }
          })
        } else {
          this.detail.columns.forEach(x => {
            if (x.field == "pms_Room_Code") {
              x.edit = { type: "selectList", keep: true };
              x.bind = { key: "pms_Room_Code", data: [] };
            }
          })
        }
        if (data && data.length) {
          data.forEach(item => {
            item.Facilities_and_Features = JSON.parse(item.Facilities_and_Features)
            item.imageAddress = item.imageAddress
            item.pms_Room_Code = JSON.parse(item.pms_Room_Code)
            item.room_picture = item.room_picture
          })
        }
      });
    },
    setHi (v) {
      let dy_hotel_ID = this.getFormOption('dy_hotel_ID')
      let distributorCode = this.getFormOption('distributorCode')
      if (v == 0) {
        distributorCode.hidden = true
        distributorCode.required = false
        dy_hotel_ID.hidden = true
        dy_hotel_ID.required = false
        this.detail.columns.forEach(i => {
          if (i.field == 'dy_room_Code') {
            i.hidden = true
          }
        })
      } else {
        distributorCode.hidden = false
        distributorCode.required = true
        dy_hotel_ID.hidden = false
        dy_hotel_ID.required = true
        this.detail.columns.forEach(i => {
          if (i.field == 'dy_room_Code') {
            i.hidden = false
          }
        })
      }
    },
    DYHomeCode (v) {
      let pram = { distributorCode: this.editFormFields.distributorCode, hotelid: v }
      this.http.post("/api/Tiktok/GetDYHomeCodeDictionary", pram).then(res => {
        if (res.status) {
          this.detail.columns.forEach(i => {
            if (i.field == 'dy_room_Code') {
              let d = res.data.map(x => {
                return {
                  key: x.room_Code,
                  value: x.room_Name
                }
              })
              i.bind.data = d
              i.edit.data = d
            }
          })
        }
      })
    },
    onInit () {

      //点击单元格编辑与结束编辑(默认是点击单元格编辑，鼠标离开结束编辑)
      this.detailOptions.clickEdit = true;
      // label宽度
      this.boxOptions.labelWidth = 140
      // 弹框宽度
      this.boxOptions.width = 1000
      // 开启单选
      //;
      // 内容超出后自动换行
      this.textInline = true;
      // 不显示多选框
      //;
      // 显示序号
      //;
      //操作从表

      let is_open = this.getFormOption('is_open')
      let distributorNews_id = this.getFormOption('distributorNews_id')
      let dy_hotel_ID = this.getFormOption('dy_hotel_ID')
      let is_dy = this.getFormOption('is_dy')
      let pram = { "page": 1, "rows": 30, "sort": "createDate", "order": "desc", "wheres": "[{\"name\":\"distributorType\",\"value\":\"2004\",\"displayType\":\"select\"}]" }
      this.http.post("/api/DistributorNews/getPageData", pram).then(res => {
        distributorNews_id.data = res.rows.map(i => {
          return {
            key: i.id,
            value: i.distributorName
          }
        })
      })

      is_open.onChange = (v) => {
        if (v == 1) {
          distributorNews_id.hidden = false
        } else {
          distributorNews_id.hidden = true
        }
      }
      is_dy.onChange = (v) => {
        this.setHi(v)
      }
      dy_hotel_ID.onBlur = (v) => {
        this.DYHomeCode(v)
      }

      this.detail.columns.push({
        title: "操作", width: 200, align: "center", fixed: 'right', render: (h, { row, column, index }) => {
          return h(
            "div",
            { style: {} },
            [h(
              "Button",
              {
                style: { "margin-right": "10px" },
                on: {
                  click: (e) => {
                    row.elementIdex = index
                    if (row.imageAddress == null || row.imageAddress == '') {
                      row.imageAddress = []
                    }
                    if (row.room_picture == null || row.room_picture == '' || row.room_picture == "[\"[]\"]") {
                      row.room_picture = []
                    }
                    if (row.Facilities_and_Features == 'null' || row.Facilities_and_Features == null) {
                      row.Facilities_and_Features = []
                    }
                    console.log("详情", row);
                    this.$refs.gridFooter.open(row)
                  }
                }
              },
              "房型详情"
            ),
            h(
              "Button",
              {
                style: { "margin-right": "10px" },
                on: {
                  click: (e) => {
                    console.log("禁启用");
                    if (this.$refs.detail.rowData[index].isAvailable == 1) {
                      this.$refs.detail.rowData[index].isAvailable = 0
                    } else {
                      this.$refs.detail.rowData[index].isAvailable = 1

                    }
                  }
                }
              },
              this.$refs.detail.rowData[index].isAvailable == 0 ? "启用" : "禁用"
            ),
              // h(
              //   "Button",
              //   {
              //     on: {
              //       click: (e) => {
              //         var  datalistChild=[]
              //         this.detail.columns.forEach(x => {
              //           if (x.field == "pms_Room_Code") {
              //             datalistChild=x.bind.data
              //             console.log(datalistChild,"datalistChild")
              //           }
              //         })
              //         this.$refs.gridBody.open(row,datalistChild)
              //       }
              //     }
              //   },
              //   "编辑"
              // ),
            ]
          )
        }
      })
      // 循环表格
      this.columns.forEach(x => {
        if (x.field == "hotel_State") {
          x.title = "酒店状态"
        }
      })

      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          let field = item.field

          if (field == "id" || field == "createDate" || field == "createID" || field == "creator" || field == "modifyDate" || field == "modifier" || field == "modifyID" || field == "supplier_Code" || field == "merchantCode" || field == "merchantName") {
            item.hidden = true
          }
          if (item.field == "hotel_State") {

            item.type = "radio"
            item.title = "酒店状态"
            item.extra = {
              tooltip: true,
              content: "1、开业:当前酒店已开业;<br/>2、停用:禁用该酒店;<br/>3、试营业:试用营业<br/>4、预开业:准备开始"
            }
          }

          // 前台电话type类型改为验证电话
          if (item.field == "reception_Phone") {
            item.type = "telephone"
          }

          if (item.field == 'HOTELDETAILADDRESS') {
            item.onChange = (key, ele) => {
              if (key != undefined) {
                this.editFormFields.hotel_Location = this.editFormFileds.HOTELDETAILADDRESS.split("|")[1]
                this.editFormFields.hotel_address = this.editFormFileds.HOTELDETAILADDRESS.split(" - ")[0]
              }
            }
          }
        });
      })
      //操作编辑框
      this.editFormOptions[2][0].extra = {
        render: h => {
          return h(
            "div",
            {
              style: {
                "border": "1px solid #dcdee2",
                "width": "200px",
                "height": "32px",
                "padding": "0px 10px"
              },
            },
            this.editFormFields.hotel_Location
          )
        }
      }
    },
    searchDetailAfter (data) {//查询从表后param查询参数,result回返查询的结果
      this.getSelListFun(data)
      // data.forEach(item => {
      //   item.Facilities_and_Features = JSON.parse(item.Facilities_and_Features)
      //   item.imageAddress = item.imageAddress
      //   item.pms_Room_Code = JSON.parse(item.pms_Room_Code)
      //   item.room_picture = item.room_picture
      // })
      // this.$Notice.success({ title: this.detailOptions.cnName + ',查询结果', desc: '返回的对象：' + JSON.stringify(data) });
      return true;
    },
    rowClick ({ row, column, event }) { //查询界面table点击行选中当前行
      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    onInited () {
      this.detailOptions.beginEdit = (row, column, index) => {
        if (row.room_Code == "" || row.room_Code == undefined) {
          console.log(row, column, index, "返回false不会进行编辑111")
          return true;//返回false不会进行编辑
        }
      }
      this.searchFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == "createDate") {
            //设置单个日期查询
            item.range = false;
            //设置查询类型为date(默认为datetime)
            item.type = "date";
          }
        })
      })
      //也可以通过配置信息所在位置直接设置
      //this.searchFormOptions[1][0].range = false;
    },
    // 从表添加行
    // addRow () {
    //   //明细表添加行，在此处可以设置添加的默认值
    //   // let obj = { anotherName:"",region:"","room_Name": "", "room_Code": "", "pms_Room_Code": "", "real_Quantity": "", "room_Introduction": "", "people_Number": "", "room_picture": [], "imageAddress": [], "room_direction": "", "room_area": "", "oldestChild": "", "remark": "", "isAvailable": 0, "Facilities_and_Features": [], "id": "", "createDate": "", "createID": "", "creator": "", "modifyDate": "", "modifier": "", "modifyID": "", "typeName": "", "typeCode": "", "latitude": "" }
    //   // //给明细表添加行时，设置默认值。--onInited方法中对this.detailOptions.columns进行formatter也可以实现)
    //   // this.$refs.detail.addRow(obj)
    //   // console.log(this.$refs.detail.addRow(obj), "222222222222")
    // },
    addRow () {
      //明细表添加行，在此处可以设置添加的默认值
      let obj = { "isAvailable": 1 }
      //给明细表添加行时，设置默认值。--onInited方法中对this.detailOptions.columns进行formatter也可以实现)
      this.$refs.detail.addRow(obj)
    },
    // 回传数据
    jsEvent (item) {
      console.log('房型的下标item.elementIdex', item.elementIdex)
      for (let key in item) {
        this.$refs.detail.rowData[item.elementIdex][key] = item[key]
      }
      console.log("房型详情回传数据", this.$refs.detail.rowData[item.elementIdex]);
      this.$refs.gridFooter.close2()
    },
    jsEvents (item) {
      console.log('父级的下标item.elementIdex', item, item.elementIdex,)
      this.$refs.detail.rowData[item.elementIdex].room_Name = item.room_Name
      this.$refs.detail.rowData[item.elementIdex].room_Code = item.room_Code
      this.$refs.detail.rowData[item.elementIdex].pms_Room_Code = item.pms_Room_Code
      this.$refs.detail.rowData[item.elementIdex].real_Quantity = item.real_Quantity
      console.log("编辑父级回传数据", this.$refs.detail.rowData, this.$refs.detail.rowData[item.elementIdex]);
      this.$refs.gridBody.close()
    },
    updateBefore (formData) { //编辑保存前formData为对象，包括明细表、删除行的Id
      if (formData.mainData.HOTELDETAILADDRESS) {
        formData.mainData.hotel_detailaddress = formData.mainData.HOTELDETAILADDRESS.split(" - ")[1].split("|")[0]
      }

      if (formData.detailData.length) {
        for (let index = 0; index < formData.detailData.length; index++) {
          const element = formData.detailData[index];
          // console.log("asd", element.name, !element.name);
          console.log("asd1", element.pms_Room_Code, !element.pms_Room_Code);
          if (!element.room_Name) {
            this.$Message.error("请输入房型名称！")
            return
          } else if (!element.room_Code) {
            this.$Message.error("请输入房型编码！")
            return
          } else if (!element.pms_Room_Code) {
            this.$Message.error("请输入pms房型代码！")
            return
          } else if (!element.real_Quantity) {
            this.$Message.error("请输入实际数量！")
            return
          }
        }
      }
      return true
    },
    addBefore (formData) {
      formData.mainData.hotel_detailaddress = formData.mainData.HOTELDETAILADDRESS.split(" - ")[1].split("|")[0]
      return true;
    },
    delBeforeDataAsync (ids, rows) {
      let hotel_ID = rows.map(i => i.hotelid)
      let obj = {
        url: '/api/Products/getPageData',
        params: {
          order: "desc",
          page: 1,
          rows: 30,
          sort: "createDate",
          wheres: `[{\"name\":\"hotel_ID\",\"value\":\"${hotel_ID.join()}\",\"displayType\":\"checkbox\"}]`
        }
      }
      return obj
    }
  }
};
export default extension;
